package user;

import utils.JsonOut;
import utils.MysqlUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "CheckNameServlet")
public class CheckNameServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        String nickname = request.getParameter("nickname");
        if (nickname == null || nickname.isEmpty()){
            out.println(JsonOut.error(201, "nickname_empty"));
            return;
        }
        String id = null;
        if ((id = (new MysqlUtils()).getUserId(nickname)) != null){

            out.println(JsonOut.common(200, "nickname_exist", id));
            return;
        }

        out.println(JsonOut.error(0, "nickname_not_exist"));
    }
}
